package com.tanhua.recommend.listener;

import cn.hutool.core.util.StrUtil;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementScore;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/11/6
 */
@Component
public class MovementListener {

    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "tanhua-movement.queue",
                    declare = "true"
            ),
            exchange = @Exchange(
                    value = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.movement"
    ))
    public void movement(Map map) {
        //解析Map集合,获得map集合中的数据
        String userId = map.get("userId").toString();
        String type = map.get("type").toString();
        String logTime = map.get("logTime").toString();
        String movementId = map.get("busId").toString();
        //通过动态id进行进行查询指定的动态
        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);
        //通过类型进行评分
        Double score = null;
        if (movement != null) {
            score = getScore(type, movement);
        }

        //封装数据,存入数据库
        MovementScore movementScore = new MovementScore();
        movementScore.setDate(System.currentTimeMillis());
        movementScore.setUserId(Long.valueOf(userId));
        movementScore.setMovementId(movement.getPid());
        movementScore.setScore(score);
        mongoTemplate.save(movementScore);


    }

    private static Double getScore(String type, Movement movement) {
        //0201为发动态  基础5分 50以内1分，50~100之间2分，100以上3分
        //0202为浏览动态， 1
        //0203为动态点赞， 5
        //0204为动态喜欢， 8
        //0205为评论，     10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢   -8
        Double score = 0d;
        switch (type) {
            case "0201":
                score = 5d;
                score += movement.getMedias().size();
                int length = StrUtil.length(movement.getTextContent());
                if (length >= 0 && length < 50) {
                    score += 1;
                } else if (length < 100) {
                    score += 2;
                } else {
                    score += 3;
                }
                break;
            case "0202":
                score = 1d;
                break;
            case "0203":
                score = 5d;
                break;
            case "0204":
                score = 8d;
                break;
            case "0205":
                score = 10d;
                break;
            case "0206":
                score = -5d;
                break;
            case "0207":
                score = -8d;
                break;
            default:
                break;
        }
        return score;
    }
}
